package com.lancoo.znbkxx.teacher.model.repository

import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken
import com.lancoo.cpbase.authentication.base.CurrentUser
import com.lancoo.znbkxx.teacher.Ext
import com.lancoo.znbkxx.teacher.Ext.Companion.BASE_URL_COMMENT
import com.lancoo.znbkxx.teacher.Ext.Companion.COMMENT_URL
import com.lancoo.znbkxx.teacher.Ext.Companion.COURSE_CLASSID
import com.lancoo.znbkxx.teacher.Ext.Companion.STAGE_NO
import com.lancoo.znbkxx.teacher.Ext.Companion.WORDS_URL
import com.lancoo.znbkxx.teacher.model.api.BaseRepository
import com.lancoo.znbkxx.teacher.model.api.ZnbkRetrofitClient
import com.lancoo.znbkxx.teacher.model.data.*
import com.lancoo.znbkxx.teacher.service.ProblemService
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody.Companion.toRequestBody

/**
 * @author : Admin
 * @date   : 2019/7/11 14:59
 * @desc   :
 */
class CommentsRepository : BaseRepository() {
    suspend fun getHardItemSolutionList(Token: String, TID: String, CourseClassID: String,
                                        SchoolID: String): ZnbkResponse<HardSolution> {
        val service = ZnbkRetrofitClient.getService(ProblemService::class.java, BASE_URL_COMMENT)
        return request {
            service.getHardItemSolutionList(Token, TID, CourseClassID, SchoolID, Ext.STAGE_NO)
        }
    }

    suspend fun getHardItemSolution(TID: String, CourseClassID: String, SchoolID: String,
                                    Token: String, QuesID: String, PaperID: String, SetID: String,
                                    StageNo: String): ZnbkResponse<HardItemSolution> {
        val service = ZnbkRetrofitClient.getService(ProblemService::class.java, BASE_URL_COMMENT)
        return request {
            service.getHardItemSolution(TID, CourseClassID, SchoolID, Token, QuesID, PaperID, SetID,
                    StageNo)
        }
    }

    suspend fun saveHardItemSolution(Token: String, TID: String, QuesID: String,
                                     CourseClassID: String, SchoolID: String,
                                     SolutionContent: String, AudioPath: String,
                                     VideoPath: String, ImagePath: String,
                                     ClassicalInfo: MutableList<Classical>,
                                     PaperName: String, State: Int,
                                     ZSDList: MutableList<ImportKlg>): ZnbkResponse<Int> {
        val map = mutableMapOf<String, Any>()
        map["Token"] = Token
        map["TID"] = TID
        map["QuesID"] = QuesID
        map["CourseClassID"] = CourseClassID
        map["SchoolID"] = SchoolID
        map["SolutionContent"] = SolutionContent
        map["AudioPath"] = AudioPath
        map["VideoPath"] = VideoPath
        map["ImagePath"] = ImagePath
        map["ClassicalInfo"] = Gson().toJson(ClassicalInfo)
        map["PaperName"] = PaperName
        map["State"] = State
        map["ZSDList"] =
                Gson().toJsonTree(ZSDList, object : TypeToken<MutableList<ImportKlg>>() {}.type)
        val mediaType = "application/json; charset=utf-8".toMediaTypeOrNull()
        val requestBody = Gson().toJson(map).toRequestBody(mediaType)
        val service = ZnbkRetrofitClient.getService(ProblemService::class.java, BASE_URL_COMMENT)
        return request { service.saveHardItemSolution(requestBody) }
    }

    suspend fun getHardQuesPassedOrFailedStu(CourseClassID: String, Key: String, QueID: String,
                                             SchoolID: String, SubjectID: String, Token: String,
                                             UserID: String): ZnbkResponse<ReviewedDetail> {
        val service = ZnbkRetrofitClient.getService(ProblemService::class.java, BASE_URL_COMMENT)
        return request {
            service.getHardQuesPassedOrFailedStu(CourseClassID, Key, QueID, SchoolID, SubjectID,
                    Token, UserID)
        }
    }

    suspend fun getClassicalList(Token: String, SchoolID: String, QuesID: String, StageNo: String,
                                 SelectedList: MutableList<Classical>,
                                 TID: String): ZnbkResponse<MutableList<Classical>> {
        val map = mutableMapOf<String, Any>()
        map["Token"] = Token
        map["SchoolID"] = SchoolID
        map["QuesID"] = QuesID
        map["StageNo"] = StageNo
        map["SelectedList"] = SelectedList
        map["TID"] = TID
        val mediaType = "application/json; charset=utf-8".toMediaTypeOrNull()
        val requestBody = Gson().toJson(map).toRequestBody(mediaType)
        val service = ZnbkRetrofitClient.getService(ProblemService::class.java, BASE_URL_COMMENT)
        return request { service.getClassicalList(requestBody) }
    }

    suspend fun getSelectedClassicalList(Token: String, SchoolID: String, QuesID: String,
                                         StageNo: String, SelectedList: MutableList<Classical>,
                                         TID: String): ZnbkResponse<MutableList<Classical>> {
        val map = mutableMapOf<String, Any>()
        map["Token"] = Token
        map["SchoolID"] = SchoolID
        map["QuesID"] = QuesID
        map["StageNo"] = StageNo
        map["SelectedList"] = SelectedList
        map["TID"] = TID
        val mediaType = "application/json; charset=utf-8".toMediaTypeOrNull()
        val requestBody = Gson().toJson(map).toRequestBody(mediaType)
        val service = ZnbkRetrofitClient.getService(ProblemService::class.java, BASE_URL_COMMENT)
        return request { service.getSelectedClassicalList(requestBody) }
    }

    suspend fun getZSDCourseware(QuesID: String): ZnbkResponse<KnowledgeInfo> {
        val map = mutableMapOf<String, Any>()
        map["Token"] = CurrentUser.Token
        map["QuesID"] = QuesID
        map["SchoolID"] = CurrentUser.SchoolID
        map["StageNo"] = STAGE_NO
        val mediaType = "application/json; charset=utf-8".toMediaTypeOrNull()
        val requestBody = Gson().toJson(map).toRequestBody(mediaType)
        val service = ZnbkRetrofitClient.getService(ProblemService::class.java, BASE_URL_COMMENT)
        return request { service.getZSDCourseware(requestBody) }
    }

    suspend fun getStuAnswerList(QuesID: String): ZnbkResponse<MutableList<AnswerDetail>> {
        val service = ZnbkRetrofitClient.getService(ProblemService::class.java, BASE_URL_COMMENT)
        return request {
            service.getStuAnswerList(STAGE_NO, CurrentUser.UserID, COURSE_CLASSID,
                    CurrentUser.SchoolID, CurrentUser.Token, QuesID)
        }
    }

    suspend fun getKlgCode(UniqueCode: String): ZnbkResponse<String> {
        val service = ZnbkRetrofitClient.getService(ProblemService::class.java, WORDS_URL)
        return request {
            service.getKlgCode(CurrentUser.Token, CurrentUser.UserID, STAGE_NO, UniqueCode)
        }
    }

    suspend fun getAnswerSkill(StuID: String, PageIndex: Int,
                               PageNum: Int): ZnbkResponse<SkillListBean> {
        val service = ZnbkRetrofitClient.getService(ProblemService::class.java, COMMENT_URL)
        return request {
            service.getAnswerSkill(CurrentUser.Token, CurrentUser.UserID, STAGE_NO,
                    Ext.SCHOOLID, StuID, PageIndex, PageNum)
        }
    }

    suspend fun getQTypeComments(StuID: String, TypeNo: String,
                                 GenreID: String): ZnbkResponse<SkillCommentInfoBean> {
        val service = ZnbkRetrofitClient.getService(ProblemService::class.java, COMMENT_URL)
        return request {
            service.getQTypeComments(CurrentUser.Token, CurrentUser.UserID, STAGE_NO,
                    Ext.SCHOOLID, StuID, TypeNo, GenreID)
        }
    }

    suspend fun getChangeExpandPracticeQues(StuID: String, TypeNo: String,
                                            GenreID: String): ZnbkResponse<MutableList<RecQuesID>> {
        val service = ZnbkRetrofitClient.getService(ProblemService::class.java, COMMENT_URL)
        return request {
            service.getChangeExpandPracticeQues(CurrentUser.Token, CurrentUser.UserID, STAGE_NO,
                    Ext.SCHOOLID, StuID, TypeNo, GenreID)
        }
    }

    suspend fun getResourceQuesInfo(QuesIDs: String, StartIndex: Int,
                                    IsSerial: Boolean,
                                    IsReplace: Boolean): ZnbkResponse<MutableList<TkModel>> {
        val service = ZnbkRetrofitClient.getService(ProblemService::class.java, COMMENT_URL)
        return request {
            service.getResourceQuesInfo(QuesIDs, STAGE_NO, StartIndex, IsSerial, IsReplace, 1)
        }
    }

    suspend fun getChangeClassicWrongQues(StuID: String, TypeNo: String,
                                          GenreID: String): ZnbkResponse<MutableList<ClassicWrongQue>> {
        val service = ZnbkRetrofitClient.getService(ProblemService::class.java, COMMENT_URL)
        return request {
            service.getChangeClassicWrongQues(CurrentUser.Token, CurrentUser.UserID, STAGE_NO,
                    Ext.SCHOOLID, StuID, TypeNo, GenreID)
        }
    }

    suspend fun getAnswerSkillList(TypeNo: String,
                                   GenreID: String): ZnbkResponse<MutableList<AnsweringSkill>> {
        val service = ZnbkRetrofitClient.getService(ProblemService::class.java, COMMENT_URL)
        return request {
            service.getAnswerSkillList(CurrentUser.Token, CurrentUser.UserID, STAGE_NO, TypeNo,
                    GenreID)
        }
    }

    suspend fun saveAnswerSkillSolution(skillCommentInfoBean: SkillCommentInfoBean, StuID: String,
                                        flag: Int): ZnbkResponse<ResultInfo> {
        val mediaType = "application/json; charset=utf-8".toMediaTypeOrNull()
        skillCommentInfoBean.Flag = flag
        skillCommentInfoBean.StuID = StuID
        skillCommentInfoBean.TID = CurrentUser.UserID
        skillCommentInfoBean.SchoolID = Ext.SCHOOLID
        val requestBody = GsonBuilder().disableHtmlEscaping().create().toJson(skillCommentInfoBean)
                .toRequestBody(mediaType)
        val service = ZnbkRetrofitClient.getService(ProblemService::class.java, COMMENT_URL)
        return request {
            service.saveAnswerSkillSolution(CurrentUser.UserID, CurrentUser.Token, requestBody)
        }
    }

    suspend fun getWrongComment(StuID: String, PageIndex: Int,
                                PageNum: Int): ZnbkResponse<WrongCommentInfo> {
        val service = ZnbkRetrofitClient.getService(ProblemService::class.java, COMMENT_URL)
        return request {
            service.getWrongComment(CurrentUser.Token, CurrentUser.UserID, CurrentUser.UserID,
                    StuID, STAGE_NO,
                    Ext.SCHOOLID, PageIndex, PageNum)
        }
    }

    suspend fun getWrongSolutionDetail(QuesID: String): ZnbkResponse<HardItemSolution> {
        val service = ZnbkRetrofitClient.getService(ProblemService::class.java, COMMENT_URL)
        return request {
            service.getWrongSolutionDetail(CurrentUser.UserID, CurrentUser.UserID, Ext.STU_ID,
                    COURSE_CLASSID,
                    Ext.SCHOOLID,
                    CurrentUser.Token, QuesID, STAGE_NO)
        }
    }

    suspend fun saveWrongSolution(Token: String, TID: String, StuID: String, QuesID: String,
                                  CourseClassID: String, SchoolID: String,
                                  SolutionContent: String, AudioPath: String,
                                  VideoPath: String, ImagePath: String,
                                  ClassicalInfo: MutableList<Classical>,
                                  PaperName: String, State: Int,
                                  ZSDList: MutableList<ImportKlg>): ZnbkResponse<ResultInfo> {
        val map = mutableMapOf<String, Any>()
        map["TID"] = TID
        map["StuID"] = StuID
        map["QuesID"] = QuesID
        map["CourseClassID"] = CourseClassID
        map["SchoolID"] = SchoolID
        map["SolutionContent"] = SolutionContent
        map["AudioPath"] = AudioPath
        map["VideoPath"] = VideoPath
        map["ImagePath"] = ImagePath
        map["ClassicalInfo"] = Gson().toJson(ClassicalInfo)
        map["PaperName"] = PaperName
        map["State"] = State
        map["ZSDList"] =
                Gson().toJsonTree(ZSDList, object : TypeToken<MutableList<ImportKlg>>() {}.type)
        val mediaType = "application/json; charset=utf-8".toMediaTypeOrNull()
        val requestBody = Gson().toJson(map).toRequestBody(mediaType)
        val service = ZnbkRetrofitClient.getService(ProblemService::class.java, BASE_URL_COMMENT)
        return request { service.saveWrongSolution(TID, Token, requestBody) }
    }
}